
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author user
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({
    "file:web/WEB-INF/dispatcher-servlet.xml",
    "file:web/WEB-INF/applicationContext.xml"
})
public class testA {

    @Test
    public void getHttpResponseMessage() throws IOException {
        String urlString   = "http://ws.qunar.com/all_lp.jcp?from=%E4%B8%8A%E6%B5%B7&to=%E5%8D%97%E5%AE%81&goDate=2012-09-27&backDate=2012-09-27&count=90&packto=2012-09-30&packreturn=2012-10-02&packcount=7&output=json&n=0.6068617776036263";;
	StringBuilder  buf = new StringBuilder();
	try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.152 Safari/535.19 CoolNovo/2.0.3.55");
            connection.setRequestProperty("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");
            connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
            connection.setRequestProperty("UA-CPU", "x86");
            connection.setRequestProperty("Accept-Encoding", "gzip");//为什么没有deflate呢
            connection.setRequestProperty("Content-type", "text/html");
            connection.setRequestProperty("Connection", "close"); //keep-Alive，有什么用呢，你不是在访问网站，你是在采集。嘿嘿。减轻别人的压力，也是减轻自己。
            connection.setUseCaches(false);//不要用cache，用了也没有什么用，因为我们不会经常对一个链接频繁访问。（针对程序）

            connection.setDoInput(true);
            connection.setRequestMethod("GET");
            if (connection.getResponseCode() == 200) {
                    InputStream stream = connection.getInputStream();
                    BufferedReader in = new BufferedReader(new InputStreamReader(stream, "UTF-8"));

                    String line;

                    while (null != (line = in.readLine())) {
                            buf.append(line).append("\n");
                    }

                    stream.close();
                    in.close();
                    connection.disconnect();
            }
	} catch (Exception e) {
		e.printStackTrace();
	}

	System.out.println(buf.toString());

        JsonFactory jsonFactory = new JsonFactory();
        JsonParser jp = jsonFactory.createJsonParser(buf.toString());

        ObjectMapper m = new ObjectMapper();
        JsonNode rootNode = m.readValue(jp, JsonNode.class);
        JsonNode outNode = rootNode.path("out").path("2012-09-30|上海-南宁").path("pr");
	System.out.println(outNode.textValue());
    }

    @Test
    public void testJsoup() throws IOException {
        Document doc = Jsoup.connect("http://www.baidu.com/").get();
        Elements es = doc.select("#nv a");
        String a = es.first().text();
        System.out.println(a);
    }

    @Test
    public void str2int() {
        String str = "1024";
        int a = Integer.parseInt(str);
        System.out.println(a);
    }

//    @Test
//    public void testJackson() {
//
//    }

}
